<template>
  <div>
    <my-breadcrumb :items="['社区管理', '用户排名']"/>
    <search :formItems="activeSearch" @search="getList" ref="search"/>
    <div id="active" style="height: 400px;"></div>
    <div class="table-active">
      <div style="text-align: center;margin: 16px 0;">
        <!-- 当前公式：发布话题{{ activeFormula.formulaone ? 'x' + activeFormula.formulaone : '' }}+点赞数{{ activeFormula.formulatwo ? 'x' + activeFormula.formulatwo : '' }}+评论数{{ activeFormula.formulathree ? 'x' + activeFormula.formulathree : '' }} -->
        当前公式：发布话题{{ welFormula.formulaone ? 'x' + welFormula.formulaone : '' }}+点赞数{{ welFormula.formulathree ? 'x' + welFormula.formulathree : '' }}+评论数{{ welFormula.formulatwo ? 'x' + welFormula.formulatwo : '' }}
        <el-button type="primary" @click="toUpdate">编辑</el-button>
      </div>
      <el-table :data="actives">
        <el-table-column
          type="index"
          label="排名"
          width="55">
        </el-table-column>
        <el-table-column
          label="用户">
          <template slot-scope="scope">
            <img :src="scope.row.avatar" alt="" style="width: 40px;height: 40px;">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="总分"
          prop="tpcount">
        </el-table-column>
        <el-table-column
          label="帖子量"
          prop="articlecount">
        </el-table-column>
        <el-table-column
          label="点赞数"
          prop="zancount">
        </el-table-column>
        <el-table-column
          label="评论数"
          prop="commentcount">
        </el-table-column>
        <!--<el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary">编辑</el-button>
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>-->
      </el-table>
    </div>

    <div class="line"></div>

    <search :formItems="popularSearch" @search="getListPop" ref="popSearch"/>
    <div id="popular" style="height: 400px;"></div>
    <div style="text-align: center;margin: 16px 0;">
      <!-- 当前公式：魅力值{{ welFormula.formulaone ? 'x' + welFormula.formulaone : '' }}+获赞值{{ welFormula.formulatwo ? 'x' + welFormula.formulatwo : '' }}+获得评论数{{ welFormula.formulathree ? 'x' + welFormula.formulathree : '' }} -->
      当前公式：魅力值{{ activeFormula.formulaone ? 'x' + activeFormula.formulaone : '' }}+获赞值{{ activeFormula.formulatwo ? 'x' + activeFormula.formulatwo : '' }}+获得评论数{{ activeFormula.formulathree ? 'x' + activeFormula.formulathree : '' }}
      <el-button type="primary" @click="toUpdate">编辑</el-button>
    </div>
    <el-table :data="populars">
      <el-table-column
        type="index"
        label="排名"
        width="55">
      </el-table-column>
      <el-table-column
        label="用户">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" style="width: 40px;height: 40px;">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="总分"
        prop="tpcount">
      </el-table-column>
      <el-table-column
        label="魅力值"
        prop="flowercount">
      </el-table-column>
      <el-table-column
        label="获赞数"
        prop="zancount">
      </el-table-column>
      <el-table-column
        label="评论数"
        prop="commentcount">
      </el-table-column>
      <!--<el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary">编辑</el-button>
          <el-button size="mini" type="danger">删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <el-dialog title="计算公式修改" :visible.sync="showUpdate">
      <h3>活跃计算公式</h3>
      <el-form :model="activeForm" label-width="70px">
        <el-form-item label="发布话题" prop="formulaone">
          <!-- activeForm.formulaone -->
          <el-input-number v-model="welForm.formulaone"/>
        </el-form-item>
        <el-form-item label="评论数" prop="formulaone">
          <el-input-number v-model="welForm.formulatwo"/>
        </el-form-item>
        <el-form-item label="点赞数" prop="formulaone">
          <el-input-number v-model="welForm.formulathree"/>
        </el-form-item>
      </el-form>
      <h3>受欢迎计算公式</h3>
      <el-form :model="welForm" label-width="70px">
        <el-form-item label="魅力值" prop="formulaone">
          <!-- welForm.formulaone -->
          <el-input-number v-model="activeForm.formulaone"/>
        </el-form-item>
        <el-form-item label="获赞值" prop="formulaone">
          <el-input-number v-model="activeForm.formulatwo"/>
        </el-form-item>
        <el-form-item label="获得评论数" prop="formulaone">
          <el-input-number v-model="activeForm.formulathree"/>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showUpdate = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getFullStrTime} from '@/utils/getList'
let echarts = require('echarts/lib/echarts')
require('echarts/lib/chart/bar')
require('echarts/lib/component/tooltip')
require('echarts/lib/component/legend')
require('echarts/lib/component/title')
export default {
  name: 'user-rank',
  data () {
    return {
      loadingObj: null,
      activeSearch: [
        {
          type: 'times-option',
          key: 'times',
          label: '时段'
        }
      ],
      actives: [],
      popularSearch: [
        {
          type: 'times-option',
          key: 'times',
          label: '时段'
        }
      ],
      populars: [],
      activeFormula: {
        formulaone: 0,
        formulatwo: 0,
        formulathree: 0
      },
      welFormula: {
        formulaone: 0,
        formulatwo: 0,
        formulathree: 0
      },
      activeForm: {
        formulaone: 0,
        formulatwo: 0,
        formulathree: 0
      },
      welForm: {
        formulaone: 0,
        formulatwo: 0,
        formulathree: 0
      },
      showUpdate: false,
      barActive: null,
      barPop: null,
      end: new Date(),
      start: null
    }
  },
  methods: {
    getList () {
      let activeChart = echarts.init(document.getElementById('active'), 'light')
      this.barActive = activeChart
      this.loadingObj = this.$Loading.service({
        target: '#active',
        text: '活跃排名生成中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      let option = {
        title: [
          {
            text: '用户活跃排名（前十）'
          },
          {
            text: '',
            bottom: 0,
            left: 'center',
            textStyle: {
              color: '#999',
              fontSize: 14,
              fontWeight: 400
            }
          }
        ],
        tooltip: {},
        legend: {
        },
        xAxis: {
          data: []
        },
        yAxis: {},
        series: [{
          name: '活跃值',
          type: 'bar',
          data: []
        }]
      }
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      this.axios.get('/ranking/index', {params: {...searchData, type: '2'}})
        .then(res => {
          if (res.data.status) {
            if (searchData.starttime) {
              option.title[1].text = `时段：${searchData.starttime}至${searchData.endtime}的用户活跃排行`
            } else {
              option.title[1].text = `时段：${this.start}至${this.end}的用户活跃排行`
            }
            this.actives = res.data.data.list
            res.data.data.list.forEach(item => {
              option.xAxis.data.push(item.name)
              option.series[0].data.push(item.tpcount)
            })
            this.loadingObj.close()
            activeChart.setOption(option)
          } else {
            this.loadingObj.close()
          }
        })
    },
    getListPop () {
      let popularChart = echarts.init(document.getElementById('popular'), 'light')
      this.barPop = popularChart
      let option2 = {
        title: [
          {
            text: '用户受欢迎排名（前十）'
          },
          {
            text: '',
            bottom: 0,
            left: 'center',
            textStyle: {
              color: '#999',
              fontSize: 14,
              fontWeight: 400
            }
          }
        ],
        tooltip: {},
        legend: {
        },
        xAxis: {
          data: []
        },
        yAxis: {},
        series: [{
          name: '受欢迎度',
          type: 'bar',
          data: []
        }]
      }
      let searchData = {}
      if (this.$refs['popSearch']) {
        searchData = this.$refs['popSearch'].search()
      }
      this.axios.get('/ranking/index', {params: {...searchData, type: '1'}})
        .then(res => {
          if (res.data.status) {
            if (searchData.starttime) {
              option2.title[1].text = `时段：${searchData.starttime}至${searchData.endtime}的用户活跃排行`
            } else {
              option2.title[1].text = `时段：${this.start}至${this.end}的用户活跃排行`
            }
            this.populars = res.data.data.list
            res.data.data.list.forEach(item => {
              option2.xAxis.data.push(item.name)
              option2.series[0].data.push(item.tpcount)
              this.loadingObj.close()
            })
            popularChart.setOption(option2)
          } else {
            this.loadingObj.close()
          }
        })
    },

    getFormula () {
      this.axios.get('/ranking/getformula')
        .then(res => {
          if (res.data.status === 1 || res.data.status === '1') {
            Object.keys(this.activeFormula).forEach(key => {
              this.activeFormula[key] = res.data.data[0][key]
            })
            Object.keys(this.welFormula).forEach(key => {
              this.welFormula[key] = res.data.data[1][key]
            })
          }
        })
    },

    toUpdate () {
      this.showUpdate = true
      this.$nextTick(() => {
        Object.keys(this.welForm).forEach(key => {
          this.welForm[key] = this.welFormula[key]
          this.activeForm[key] = this.activeFormula[key]
        })
      })
    },
    toSubmit () {
      let params = {data: [{id: '1'}, {id: '2'}]}
      Object.keys(this.welForm).forEach(key => {
        params.data[0][key] = this.welForm[key]
        params.data[1][key] = this.activeForm[key]
      })
      this.axios.post('/ranking/editformula', this.qs.stringify(params))
        .then(res => {
          if (res.data.status === 1) {
            this.$message.success(res.data.msg)
            this.getFormula()
            this.showUpdate = false
          }
        })
    }
  },
  mounted () {
    this.getList()
    this.getListPop()
  },
  created () {
    this.getFormula()
    let _self = this
    this.start = new Date(this.end.getFullYear(), this.end.getMonth(), this.end.getDate() - this.end.getDay() + 1)
    this.start = getFullStrTime(this.start)
    this.end = getFullStrTime(this.end)
    window.onresize = function () {
      _self.barActive.resize()
      _self.barPop.resize()
    }
  }
}
</script>

<style scoped>
  .table-active{
    margin: 16px 0 24px;
  }
  .line{
    height: 24px;
    border-top: 1px solid #666;
  }
</style>
